<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="addMenu">增加按钮</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script src="js/jquery-2.2.3.min.js"></script>
<script>
    layui.use('table', function(){
        var table = layui.table;

        //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
        //详见：https://www.layui.com/doc/modules/table.html#totalRow
        table.render({
            elem: '#test'
            ,url:'/getMenuList'
            ,method:'post'
            ,toolbar: '#toolbarDemo'
            ,title: '菜单数据表'
            ,totalRow: true
            ,cols: [
                [
                    {type: 'checkbox', fixed: 'left', totalRowText: '合计'}
                    ,{field:'id', title:'菜单编号', width:150, fixed: 'left', sort: true, totalRow: true}
                    ,{field:'url', title:'链接地址', width:150}
                    ,{field:'mtext', title:'菜单名称', width:150}
                    ,{field:'ordernum', title:'菜单优先级', width:150, sort: true}
                    ,{field:'ismenu', title:'菜单类型', width:150, }
                    ,{field:'parentid', title:'上级菜单', width:150}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]
            ]
            ,page: true
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'addMenu':
                    addMenu();
                    break;
            };
        });

        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.alert('编辑行：<br>'+ JSON.stringify(data))
            }
        });

    });
    function addMenu() {
        layer.open({
            type: '1',
            content: $('.registerPage'),
            title: '注册',
            area: ['530px', '550px'],
            btn: ['注册', '重置', '取消'],
            closeBtn: '1',
            btn1: function (index, layero) {
                //注册回调
                layer.close(index);
                var registerUsername = $('#registerUsername').val();
                var registerPassword = $('#registerPassword').val();
                var registerWellPassword = $('#registerWellPassword').val();
                var tele = $('#tele').val();
                var wechat = $('#wechat').val();
                var selectValue = $('#roleSelect option:selected').val();
                var params = {};
                params.uname = registerUsername;
                params.upass = registerPassword;
                params.checkPwd = registerWellPassword;
                params.tele = tele;
                params.wechat = wechat;
                params.roleid = selectValue;
                var registerLoadIndex = layer.load(2);
                $.ajax({
                    type: 'post',
                    // url: window.location.protocol + '//' + window.location.host + '/security-web/register.do',
                    url: "/register",
                    dataType: 'json',
                    data: JSON.stringify(params),
                    contentType: 'application/json',
                    success: function (data) {
                        layer.close(registerLoadIndex);
                        layer.msg(data.msg);
                    },
                    error: function () {
                        layer.close(registerLoadIndex);
                        layer.alert("请求超时！")
                    }
                });
            },
            btn2: function (index, layero) {
                //重置回调
                var registerUsername = $('#registerUsername').val("");
                var registerPassword = $('#registerPassword').val("");
                var registerWellPassword = $('#registerWellPassword').val("");
                // 防止注册页面关闭
                return false;
            },
            btn3: function (index, layero) {
                //取消回调
            }
        })

    };
</script>

</body>
<!--增加菜单-->
<!--<div class="registerPage">-->
<!--    <div class="registerDiv">-->
<!--        <form>-->
<!--            <div class="usernameWrapDiv">-->
<!--                <div class="usernameLabel">-->
<!--                    <label>用户名:</label>-->
<!--                </div>-->
<!--                <div class="usernameDiv">-->
<!--                    <i class="layui-icon layui-icon-username adminIcon"></i>-->
<!--                    <input id="registerUsername" class="layui-input adminInput" type="text" name="username"-->
<!--                           placeholder="输入用户名">-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="usernameWrapDiv">-->
<!--                <div class="usernameLabel">-->
<!--                    <label>密码:</label>-->
<!--                </div>-->
<!--                <div class="passwordDiv">-->
<!--                    <i class="layui-icon layui-icon-password adminIcon"></i>-->
<!--                    <input id="registerPassword" class="layui-input adminInput" type="password" name="password"-->
<!--                           placeholder="输入密码">-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="usernameWrapDiv">-->
<!--                <div class="usernameLabel">-->
<!--                    <label>确认密码:</label>-->
<!--                </div>-->
<!--                <div class="passwordDiv">-->
<!--                    <i class="layui-icon layui-icon-password adminIcon"></i>-->
<!--                    <input id="registerWellPassword" class="layui-input adminInput" type="password" name="password"-->
<!--                           placeholder="输入密码">-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="usernameWrapDiv">-->
<!--                <div class="usernameLabel">-->
<!--                    <label>手机号:</label>-->
<!--                </div>-->
<!--                <div class="usernameDiv">-->
<!--                    <i class="layui-icon layui-icon-username adminIcon"></i>-->
<!--                    <input id="tele" class="layui-input adminInput" type="text" name="tele"-->
<!--                           placeholder="输入手机号">-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="usernameWrapDiv">-->
<!--                <div class="usernameLabel">-->
<!--                    <label>微信号:</label>-->
<!--                </div>-->
<!--                <div class="usernameDiv">-->
<!--                    <i class="layui-icon layui-icon-username adminIcon"></i>-->
<!--                    <input id="wechat" class="layui-input adminInput" type="text" name="wechat"-->
<!--                           placeholder="输入微信号">-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="usernameWrapDiv">-->
<!--                <div class="usernameLabel">-->
<!--                    <label>角色类型:</label>-->
<!--                </div>-->
<!--                <div class="passwordDiv">-->
<!--                    <select id="roleSelect" class="layui-select">-->
<!--                        <option value="" selected disabled>请选择</option>-->
<!--                        <option value="1">管理员</option>-->
<!--                        <option value="2">用户</option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->
<!--        </form>-->
<!--    </div>-->
<!--</div>-->
</html>